package com.hongxun.net;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongxun.interfaces.NovaCallback;
import com.hongxun.budget.utils.L;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxStringCallback;

import dmax.dialog.SpotsDialog;

/**
 * Created by admin on 2018/1/29.
 */

public class Callback extends RxStringCallback {

    NovaCallback c;
    SpotsDialog d;
    int i;

    public Callback(int i, NovaCallback c, SpotsDialog d) {
        this.c = c;
        this.d = d;
        this.i = i;
    }

    @Override
    public void onNext(Object tag, String response) {
        dissmiss();
        if (!TextUtils.isEmpty(response)) {
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code") != 0) {
                c.err(i, jsonObject.getInteger("code"), jsonObject.getString("msg"));
            } else {
                c.success(i, jsonObject);
            }
        } else {
            c.err(i, -999, "未知错误，请联系管理员!");
        }
    }

    @Override
    public void onError(Object tag, Throwable e) {
        L.d("nova", tag + "---" + e.getMessage());
        c.err(i, -998, e.getMessage());
        dissmiss();
    }

    @Override
    public void onCancel(Object tag, Throwable e) {
        L.e("nova", e.getMessage());
        c.err(i, -1000, e.getMessage());
        dissmiss();
    }

    void dissmiss() {
        if (d != null) {
            d.dismiss();
            d.cancel();
        }
    }

}
